<template>
  <div>
    <div class="main">
      <van-field
        readonly
        class="input_text"
        name="ChnName"
        v-model="form.ChnName"
        label="姓名"
        placeholder="请选择"
        @click="goPeopleList"
        required
      >
      </van-field>
      <van-field label="身份证号" v-model="form.CardNo" name="CardNo" required>
      </van-field>

      <van-field
        label="社保编号"
        v-model="form.SocialSecurityNo"
        name="SocialSecurityNo"
      >
      </van-field>

      <van-field
        readonly
        class="input_text"
        name="form.LeaveDate"
        v-model="form.LeaveDate"
        label="离职日期"
        placeholder="请选择"
        @click="handleShow"
      >
      </van-field>

      <van-field
        readonly
        class="input_text"
        name="form.SocialSecurityDate"
        v-model="form.SocialSecurityDate"
        label="参保日期"
        placeholder="请选择"
        @click="handleShow1"
      >
      </van-field>

      <van-field class="input_text" name="radio" label="员工状态" required>
        <template #input>
          <van-radio-group
            @change="radioChange"
            direction="horizontal"
            v-model="form.EmpStatus"
          >
            <template v-for="(item, index) in form.EmpStatusList">
              <van-radio :name="item.ID" :key="index">{{
                item.Name
              }}</van-radio>
            </template>
          </van-radio-group>
        </template>
      </van-field>

      <van-field
        label="缴费基数"
        v-model="form.BaseSalary"
        name="BaseSalary"
      >
      </van-field>

      <div class="btn_submit">
        <van-button block type="info" @click="submit"> 提交 </van-button>
      </div>
    </div>

    <!-- <van-calendar color="#1989fa" v-model="showCalendar" @confirm="onConfirm" /> -->
    <van-calendar
      color="#1989fa"
      v-model="showCalendar"
      :min-date="new Date('2000-01-01')"
        :max-date="new Date('2040-01-01')"
      :default-date="tempDate"
      @confirm="onConfirm"
    />

    <!-- 员工 -->
    <ExaminePeople
       title=""
      :show.sync="userPopup"
      :selectedValue="form.UserName"
      @callback="_chooseUserPost"
    ></ExaminePeople>
  </div>
</template>

<script>
import {
  SiteInjuryRecordInitEntity,
  SiteInjuryRecordAddSubmit,
  SiteInjuryRecordGetDetail,
} from "@/api/SiteSafeCost";
import { uploadAttach } from "@/api/common";
import { formatDate, getDate, getTime } from "@/utils/getDate";
import ExaminePeople from "@/views/components/user/userChooseDialog.vue";
import { mapGetters } from "vuex";
import store from '@/store/index'

export default {
  components: {
    ExaminePeople,
  },
  data() {
    return {
      id: this.$route.query.id,
      radio: "1",
      form: {},
      userPopup: false,
      filePopup: false,
      showCalendar: false,
      seleCalendarType: 0,
      tempDate: new Date(),
    };
  },

  computed: {
    ...mapGetters(["baseUrl", "CompanyCode", "roles"]),
  },

  created() {
    if (this.$route.query.id != null) {
      (this.id = this.$route.query.id), this.getDetail();
    }

    this.calcTitle();

  

    //this.getDistList();
  },
  activated() {
    // if (this.$route.params.certFile) {
    // 	this.fileList.push({
    // 		AttCode: this.$route.params.certFile.AttCode,
    // 		AttExt: this.$route.params.certFile.AttExt,
    // 		AttName: this.$route.params.certFile.AttName,
    // 	})
    // }
    //将该页面的id和新获取的id进行比对，如果不一致，说明页面应该进行数据刷新
    //重新请求数据
    // if (this.$route.params.userlist) {
    //   const userlist = this.$route.params.userlist;
    //   let EquManagerChnName = [];
    //   let EquManagerUserName = [];
    //   userlist.forEach((item) => {
    //     EquManagerChnName.push(item.name);
    //     EquManagerUserName.push(item.code);
    //   });
    //   this.form.EquManagerChnName = EquManagerChnName.join(",");
    //   this.form.EquManagerUserName = EquManagerUserName.join(",");
    // }
  },

  methods: {

    calcTitle() {
      let title = "工商保险台账[新增]";
      if (this.id == "0") {
        title = "工商保险台账[新增]";
      } else {
        title = "工商保险台账[编辑]";
      }
      store.commit("updateTitle", title);
    },

    getDetail(certId) {
      let params = { id: this.id };

      if (this.id == 0) {
        SiteInjuryRecordInitEntity(params).then((res) => {
          if (res.data.Attachs) {
            let Attachs = res.data.Attachs.split(";");
            console.log(Attachs, "Attachs");
            res.data.Attachs = JSON.parse(res.data.Attachs);
          } else {
            res.data.Attachs = [];
          }

          this.form = res.data;
          this.form.LeaveDate = getDate(this.form.LeaveDate);
          this.form.SocialSecurityDate = getDate(this.form.SocialSecurityDate);
        });
      } else {
        SiteInjuryRecordGetDetail(params).then((res) => {
          
          if (res.data.Attachs) {
            let Attachs = res.data.Attachs.split(";");
            console.log(Attachs, "Attachs");
            res.data.Attachs = JSON.parse(res.data.Attachs);
          } else {
            res.data.Attachs = [];
          }
          this.form = res.data;
          this.form.LeaveDate = getDate(this.form.LeaveDate);
          this.form.SocialSecurityDate = getDate(this.form.SocialSecurityDate);
        });
      }
    },

    submit() {
      if (!this.form.ChnName) {
        return this.$toast("请选择姓名");
      }

      if (!this.form.CardNo) {
        return this.$toast("请输入身份证号");
      }

      if (!this.form.EmpStatus) {
        return this.$toast("请选择员工状态");
      }

//       id	77
// CompanyCode	00001
// Code	SIR0000120210300001
// ChnName	apple
// CardNo	12345678765432
// SocialSecurityNo	324567767543
// LeaveDate	2021-03-31
// SocialSecurityDate	2021-03-20
// EmpStatus	1
// BaseSalary	1500.0

      // if(this.form){
      let params = {
        id: this.form.ID,
        CompanyCode: this.CompanyCode,
        Code: this.form.Code,
        ChnName: this.form.ChnName,
        CardNo: this.form.CardNo,
        SocialSecurityNo: this.form.SocialSecurityNo,
        LeaveDate:this.form.LeaveDate,
        SocialSecurityDate:this.form.SocialSecurityDate,
        EmpStatus: this.form.EmpStatus,
        BaseSalary: this.form.BaseSalary,
      };

      SiteInjuryRecordAddSubmit(params).then((res) => {
        if (this.id == 0) {
          this.$toast.success("新增成功");
        } else {
          this.$toast.success("保存成功");
        }

        this.form = {
          ID: 0,
        };
        this.$router.go(-1);
        // setTimeout(() => {
        //   // this.$router.push("/Site_Appointment");
        //   history.go(-1);
        // }, 500);
      });
    },

    handleShow() {
      self.seleCalendarType = 0;
      this.showCalendar = true;
    },

    handleShow1() {
      self.seleCalendarType = 1;
      this.showCalendar = true;
    },

    onConfirm(date) {
      const dateTime = formatDate(date);
      if (self.seleCalendarType == 0) {
        this.form.LeaveDate = getDate(dateTime);
      } else {
        this.form.SocialSecurityDate = getDate(dateTime);
      }

      this.showCalendar = false;
    },


    radioChange(names) {
      this.form.EmpStatus = names;
    },

    // 附件详情
    _goEnclosure(item) {
      // console.log(item,'sdvfdvdcd')
      this.$router.push({
        path: "/Credential/Enclosure",
        query: { attCode: item.AttCode },
      });
    },

    goPeopleList() {
      this.userPopup = true;
    },

    _chooseUserPost(item) {
      if (item) {
        this.$set(this.form, "ChnName", item.ChnName);
        this.$set(this.form, "UserName", item.UserName);
        this.userPopup = false;
      }
    },
  },
};
</script>

<style lang="less" >
.wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
}

.block {
  width: 120px;
  height: 120px;
  background-color: #fff;
}
</style>
<style lang="less" scoped>
.space-between {
  justify-content: space-between;
}
</style>
